<template>
  <view class="detail-container">
    <scroll-view scroll-y class="detail-scroll">
      <!-- 商品图片轮播 -->
      <swiper class="detail-swiper" indicator-dots autoplay circular>
        <swiper-item v-for="(img, index) in product.image" :key="index">
          <image :src="img" class="swiper-img" mode="aspectFill" />
        </swiper-item>
      </swiper>
      
      <!-- 商品基本信息 -->
      <view class="basic-info">
        <view class="info-header">
          <text class="product-name">{{ product.name }}</text>
          <view class="status-tag" :class="{'on-sale': product.status === 1}">
            {{ product.status === 1 ? '出售中' : '已下架' }}
          </view>
        </view>
        
        <view class="price-section">
          <text class="price">¥{{ product.price.toLocaleString() }}</text>
        </view>
        
        <view class="info-grid">
          <view class="info-item">
            <text class="label">库存</text>
            <text :class="['value', {'warning': product.stock < 3}]">
              {{ product.stock }}件
            </text>
          </view>
          
          <view class="info-item">
            <text class="label">材质</text>
            <text class="value">{{ product.material }}</text>
          </view>
          
          <view class="info-item">
            <text class="label">分类</text>
            <text class="value">{{ product.category || '未分类' }}</text>
          </view>
          
          <view class="info-item">
            <text class="label">创建时间</text>
            <text class="value">{{ formatDate(product.create_date) }}</text>
          </view>
        </view>
      </view>
      
      <!-- 详细介绍 -->
      <view class="detail-section">
        <text class="section-title">详细介绍</text>
        <rich-text :nodes="product.description || '暂无详细介绍'"></rich-text>
      </view>
    </scroll-view>
    
    <!-- 底部操作栏 -->
    <view class="action-bar">
      <button class="edit-btn" @click="editProduct">编辑</button>
      <button 
        class="sell-btn" 
        v-if="product.status === 1"
        @click="navToSell"
      >
        出售
      </button>
      <button 
        class="status-btn" 
        :class="{'off-sale': product.status === 1}" 
        @click="toggleStatus"
      >
        {{ product.status === 1 ? '下架' : '上架' }}
      </button>
    </view>
  </view>
</template>

<script>
const db = uniCloud.database();

export default {
  data() {
    return {
      product: {
        name: '',
        price: 0,
        stock: 0,
        material: '',
        image: [],
        description: '',
        status: 1,
        create_date: Date.now()
      }
    };
  },
  onLoad(options) {
    if (options.id) {
      this.loadProductDetail(options.id);
    }
  },
  methods: {
    async loadProductDetail(id) {
      uni.showLoading({ title: '加载中' });
      
      try {
        const res = await db.collection('furniture_inventory').doc(id).get();
        if (res.result.data.length) {
          this.product = res.result.data[0];
          // 确保image是数组
          if (typeof this.product.image === 'string') {
            this.product.image = [this.product.image];
          }
        } else {
          uni.showToast({
            title: '商品不存在',
            icon: 'none'
          });
          setTimeout(() => {
            uni.navigateBack();
          }, 1500);
        }
      } catch (e) {
        uni.showToast({
          title: '加载失败: ' + e.message,
          icon: 'none'
        });
      } finally {
        uni.hideLoading();
      }
    },
    
    formatDate(timestamp) {
      const date = new Date(timestamp);
      return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`;
    },
    
    editProduct() {
      uni.navigateTo({
        url: `/pages/inventory/edit?id=${this.product._id}`
      });
    },
    
    navToSell() {
      uni.navigateTo({
        url: `/pages/inventory/quick-sell?id=${this.product._id}`
      });
    },
    
    async toggleStatus() {
      const newStatus = this.product.status === 1 ? 0 : 1;
      
      try {
        await db.collection('furniture_inventory')
          .doc(this.product._id)
          .update({
            status: newStatus
          });
          
        this.product.status = newStatus;
        
        uni.showToast({
          title: `商品已${newStatus === 1 ? '上架' : '下架'}`,
          icon: 'success'
        });
        
      } catch (e) {
        uni.showToast({
          title: '操作失败: ' + e.message,
          icon: 'none'
        });
      }
    }
  }
};
</script>

<style lang="scss">
.detail-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-color: #f9f5f0;
  
  .detail-scroll {
    flex: 1;
    padding-bottom: 100rpx;
  }
  
  .detail-swiper {
    height: 500rpx;
    
    .swiper-img {
      width: 100%;
      height: 100%;
      background-color: #f0e6d8;
    }
  }
  
  .basic-info {
    background: white;
    padding: 30rpx;
    margin: 20rpx;
    border-radius: 20rpx;
    box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.05);
    
    .info-header {
      display: flex;
      align-items: center;
      margin-bottom: 20rpx;
      
      .product-name {
        font-size: 36rpx;
        font-weight: bold;
        color: #5D4037;
        flex: 1;
      }
      
      .status-tag {
        background: #999;
        color: white;
        padding: 6rpx 15rpx;
        border-radius: 20rpx;
        font-size: 24rpx;
        
        &.on-sale {
          background: #67C23A;
        }
      }
    }
    
    .price-section {
      display: flex;
      align-items: baseline;
      margin-bottom: 30rpx;
      
      .price {
        font-size: 44rpx;
        font-weight: bold;
        color: #d42328;
        margin-right: 20rpx;
      }
    }
    
    .info-grid {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      gap: 20rpx;
      
      .info-item {
        .label {
          font-size: 28rpx;
          color: #999;
          display: block;
          margin-bottom: 8rpx;
        }
        
        .value {
          font-size: 30rpx;
          color: #333;
          font-weight: 500;
          
          &.warning {
            color: #e6a23c;
            font-weight: bold;
          }
        }
      }
    }
  }
  
  .detail-section {
    background: white;
    margin: 20rpx;
    padding: 30rpx;
    border-radius: 20rpx;
    box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.05);
    
    .section-title {
      font-size: 32rpx;
      font-weight: bold;
      color: #5D4037;
      display: block;
      margin-bottom: 25rpx;
      position: relative;
      padding-left: 20rpx;
      
      &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 50%;
        transform: translateY(-50%);
        width: 6rpx;
        height: 30rpx;
        background: #8B4513;
        border-radius: 3rpx;
      }
    }
  }
  
  .action-bar {
    display: flex;
    padding: 20rpx 30rpx;
    background: white;
    border-top: 1rpx solid #eee;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 100;
    
    button {
      flex: 1;
      margin: 0 15rpx;
      border-radius: 50rpx;
      height: 80rpx;
      line-height: 80rpx;
      font-size: 32rpx;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    
    .edit-btn {
      background: #f8f8f8;
      color: #333;
      border: none;
    }
    
    .sell-btn {
      background: #8B4513;
      color: white;
      border: none;
    }
    
    .status-btn {
      background: #f8f8f8;
      color: #333;
      border: none;
      
      &.off-sale {
        background: #fef0f0;
        color: #f56c6c;
      }
    }
  }
}
</style>